package com.slg.android.manyanime.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.slg.android.manyanime.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by E540 on 2015/3/10.
 */
public class NavigationBar extends LinearLayout implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private List<RadioButton> buttons;
    private CheckChangedListener listener;
    private Context context;

    public NavigationBar(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public NavigationBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context);
    }

    public void setNavigationContent(String[] contents) {
        if (contents == null || contents.length == 0) {
            throw new IllegalArgumentException("必须设置导航栏内容");
        }
        buttons = new ArrayList<>();
        for (int i = 0; i < contents.length; i++) {
            RadioButton radioButton = new RadioButton(context);
            radioButton.setLayoutParams(new ViewGroup.LayoutParams(200
                    , ViewGroup.LayoutParams.MATCH_PARENT));
            radioButton.setText(contents[i]);
            radioButton.setButtonDrawable(null);
            if (i == 0) {
                radioButton.setChecked(true);
                radioButton.setTextColor(Color.WHITE);
                radioButton.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.navigation_bar_item_bg));
            } else {
                radioButton.setChecked(false);
                radioButton.setTextColor(getResources().getColor(R.color.main_menu_text));
                radioButton.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.navigation_bar_item_checked_bg));
            }
            buttons.add(radioButton);
            radioGroup.addView(radioButton, i);
        }


    }

    private void init(Context context) {
        radioGroup = new RadioGroup(context);
        radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        radioGroup.setLayoutParams(
                new RadioGroup.LayoutParams(600
                        ,150));
        radioGroup.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.navigation_bar_parent_bg));
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton child = (RadioButton) group.getChildAt(i);
            child.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.navigation_bar_item_bg));
            child.setTextColor(getResources().getColor(R.color.main_menu_text));
            if (child.isChecked()) {
                child.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.navigation_bar_item_checked_bg));
                child.setTextColor(Color.WHITE);
                listener.onCheckChanged(i);
            }
        }
    }

    public void setListener(CheckChangedListener listener) {
        this.listener = listener;
    }

    public interface CheckChangedListener {
        void onCheckChanged(int position);
    }
}
